/*
 * 
 * 
 * 
 */
package com.cms.controller.admin;

import com.cms.domain.Config;
import com.cms.domain.Feedback;
import com.cms.entity.Setup;
import com.cms.service.SetupService;
import com.cms.utils.CacheUtils;
import com.cms.utils.SystemUtils;
import com.cms.utils.WebUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;


/**
 * Controller - 配置
 * 
 * 
 * 
 */
@Controller("adminSetupController")
@RequestMapping("/admin/setup")
public class SetupController extends BaseController {

	@Autowired
	private SetupService setupService;
	
	/**
	 * 编辑
	 */
	@RequestMapping
	public String index(ModelMap modelMap){
		Config config = SystemUtils.getConfig();
		modelMap.addAttribute("config", config);
		return getView("setup/index");
	}
	
	/**
	 * 更新
	 */
	@ResponseBody
	@RequestMapping("/update")
	public Feedback update(){
		Map<String,String> setupMap = setupService.getSetupMap();
		Set<String> keys = setupMap.keySet();
		for(String key : keys){
			String value = setupMap.get(key);
			String paraValue = WebUtils.getRequest().getParameter(key);
			if(!StringUtils.equals(value, paraValue)){
				Setup setup = setupService.findByName(key);
				setup.setValue(paraValue);
				setupService.updateById(setup);
			}
		}
		CacheUtils.clearConfig();
		return Feedback.success(new HashMap<>());
	}
	
}
